<?php
namespace App\Services\Admin;

use Illuminate\Support\Facades\DB;

class QrService extends BaseService
{
    /**
     * 二维码
     */

    /**
     * 码列表
     * @param array $pages【start起始页面，limit每页记录数】
     * @param array $pas【isuse，scene_id】
     */
    public function getList($pages, $pas)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbQr);
        if ($pas['isuse']) {
            if ($pas['isuse'] == 1) {
                $db = $db->where('tmp_id', 0);
            } else {
                $db = $db->where('tmp_id', '>', 0);
            }
        }
        if ($pas['scene_id']) {
            $db = $db->where('scene_id', $pas['scene_id']);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有了');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'code' => $v['code'],
                'show_code' => $v['show_code'],
                'tmp_id' => $v['tmp_id'],
                'prefix' => env('DOMAIN').'/h5/?code=',
            ];
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $arys, 'total' => $total);
    }

    /**
     * 扫码列表
     * @param string $code 码值
     * @param string $cuode 用户标识
     * @param array $pages【start起始页面，limit每页记录数】
     * @return array
     */
    public function getLogList($code, $ucode, $pages)
    {
        $db = $this->dbConn($this->dbQrlog);
        if ($code) $db = $db->where('code', $code);
        if ($ucode) $db = $db->where('ucode', $ucode);
        $start = $pages['start'] * $pages['limit'];
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return array('code' => -2, 'msg' => '没有了');
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }

    /**
     * 批量生成码
     * @param int $num 生成的数量
     * @return array
     */
    public function batchAdd($num = 1)
    {
        for ($i=0; $i<$num; $i++) {
            $showCode = time().rand(10000, 99999);
            $qrDatas = [
                'code' => $this->getCode(),
                'show_code' => $showCode,
                'addtime' => toDate(),
            ];
            $this->dbConn($this->dbQr)->insertGetId($qrDatas);
        }
        return $this->toback();
//        DB::beginTransaction(); //开启事务
//        try {
//            $lastId = $this->db->addOne($qrDatas);
//            DB::commit(); //提交事务
//            return array('code'=>0, 'msg'=>'success', 'data'=>$lastId);
//        } catch (\Exception $e) {
//            DB::rollBack(); //事务回滚
//            echo $e->getMessage();
//            return array('code'=>-1, 'msg'=>'操作失败');
//        }
    }

    //生成新码
    public function getCode()
    {
        $code = toRandStr(rand(4, 6), 1);
        $qrOne = $this->dbConn($this->dbQr)
            ->select('id as codeId', 'code', 'view_type', 'view', 'useNum')
            ->where('code', $code)
            ->first();
        if ($qrOne) {
            return $this->getCode();
        }
        return $code;
    }

    //补全showcode
    public function modifyShowCode()
    {
        $ones = $this->dbConn($this->dbQr)->where('show_code', '')->get();
        if (empty($ones)) return 'no data';
        foreach ($ones as $v) {
            $qrData = [
                'show_code' => time().rand(10000, 99999),
            ];
            $this->dbConn($this->dbQr)->where('id', $v->id)->update($qrData);
        }
        return 'OK';
    }
}